package com.stone;/*

 */

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

/**
 * 创建索引
 * @ClassName EsTest_Client
 * @Author Final
 * @Project DataStructures
 */
public class EsTest_Doc_Create_Batch {

    public static void main(String[] args) throws IOException {

        //创建客户端
        RestHighLevelClient esClient=new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );

        BulkRequest request=new BulkRequest();
        request.add(new IndexRequest().index("students").id("1001").source(XContentType.JSON,"name","张三","age","134"));
        request.add(new IndexRequest().index("students").id("1002").source(XContentType.JSON,"name","张四","age","14"));
        request.add(new IndexRequest().index("students").id("1003").source(XContentType.JSON,"name","张五","age","34"));
        request.add(new IndexRequest().index("students").id("1004").source(XContentType.JSON,"name","张六","age","753"));
        request.add(new IndexRequest().index("students").id("1005").source(XContentType.JSON,"name","张七","age","34"));
        request.add(new IndexRequest().index("students").id("1006").source(XContentType.JSON,"name","张八","age","753"));

        BulkResponse respone = esClient.bulk(request, RequestOptions.DEFAULT);
        System.out.println(respone.getTook());
        System.out.println(respone.getItems());

        //关闭es客户端
        esClient.close();
    }
}
